Conditions | 9 |
Paths | 44 |
Total Lines | 94 |
Lines | 0 |
Ratio | 0 % |
Changes | 24 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | const elementResizeDetectorMaker = require('element-resize-detector'); |
||
4 | constructor(el, cb) { |
||
5 | const _this = this; |
||
6 | |||
7 | _this.$el = el ? el : window; |
||
8 | _this.$img = _this.getElementForSizing(); |
||
9 | _this.disableOnMobile = _this.$el.dataset['cover-image-mobile'] === 'false'; |
||
10 | _this.cb = cb || (() => { |
||
11 | //DEBUG console.log("Default callback"); |
||
12 | }); |
||
13 | |||
14 | _this.positioning = { |
||
15 | x : 0.5, |
||
16 | y : 0.5 |
||
17 | }; |
||
18 | _this.options = { |
||
19 | parallax : _this.$el.dataset.coverImageParallax === 'true' |
||
20 | }; |
||
21 | |||
22 | if (!_this.$img) { |
||
23 | console.log('Error:', 'no image found', _this.$img ); |
||
|
|||
24 | return; |
||
25 | } |
||
26 | |||
27 | _this.imageWidth = _this.$img.getAttribute('width'); |
||
28 | _this.imageHeight = _this.$img.getAttribute('height'); |
||
29 | |||
30 | // If the image doesn't have harcoded width|height |
||
31 | // attributes then load the image to calculate |
||
32 | // the dimensions |
||
33 | if (!_this.imageWidth || !_this.imageHeight) { |
||
34 | console.log('No dimensions found. Generating image:', _this.$img.src) |
||
35 | _this.img = new Image(); |
||
36 | _this.img.src = _this.$img.src; |
||
37 | |||
38 | _this.imageWidth = _this.img.width; |
||
39 | _this.imageHeight = _this.img.height; |
||
40 | } |
||
41 | |||
42 | if ( _this.disableOnMobile && window.innerWidth < 480 ) { |
||
43 | return; |
||
44 | } |
||
45 | |||
46 | _this.elementDimensions = { |
||
47 | height : _this.$el.clientHeight, |
||
48 | width : _this.$el.clientWidth |
||
49 | }; |
||
50 | |||
51 | _this.$el.style = ` |
||
52 | overflow : hidden; |
||
53 | position : relative; |
||
54 | `; |
||
55 | |||
56 | if (!_this.$img) { |
||
57 | // TODO: Implement load |
||
58 | setTimeout( () => { |
||
59 | new CoverImage( _this.$el ); |
||
60 | }, 1000); |
||
61 | |||
62 | } else { |
||
63 | _this.resizeImage(); |
||
64 | } |
||
65 | |||
66 | _this.$img.addEventListener('load', () => { |
||
67 | _this.resizeImage(); |
||
68 | }, false); |
||
69 | |||
70 | _this.$el.addEventListener('transitionend', () => { |
||
71 | _this.resizeImage(); |
||
72 | }, false) |
||
73 | |||
74 | var erd = elementResizeDetectorMaker({ |
||
75 | strategy: 'scroll' |
||
76 | }); |
||
77 | |||
78 | erd.listenTo(_this.$el, () => { |
||
79 | _this.resizeImage(); |
||
80 | }) |
||
81 | |||
82 | _this.$el.addEventListener('animationend', () => { |
||
83 | _this.resizeImage(); |
||
84 | }, false) |
||
85 | |||
86 | window.addEventListener('resize', () => { |
||
87 | _this.resizeImage(); |
||
88 | }, true); |
||
89 | |||
90 | window.addEventListener('ci.resize', () => { |
||
91 | _this.resizeImage(); |
||
92 | }, true); |
||
93 | |||
94 | if (_this.options.parallax) { |
||
95 | _this.draw(); |
||
96 | } |
||
97 | } |
||
98 | |||
221 | }); |